define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mp_mp/index',
                    add_url: 'mp_mp/add',
                    edit_url: 'mp_mp/edit',
                    del_url: 'mp_mp/del',
                    multi_url: 'mp_mp/multi',
                    dragsort_url: 'ajax/weigh',
                    table: 'mp_mp',
                }
            });
            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'add_time',
                search: false,
                pageSize: 10,
                columns: [
                    [
                        //{checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'username', title: __('姓名')},
                        {field: 'phone', title: __('手机号')},
                        {field: 'login_source', title: __('来源'), formatter: Table.api.formatter.status, searchList: {'dy': '抖音', 'xcx': '微信小程序'}},
                        {field: 'company', title: __('公司')},
                        {field: 'position', title: __('职位')},
                        {field: 'mail', title: __('邮箱')},
                        {field: 'address', title: __('地址')},
                        {
                            field: 'status',
                            title: __('状态'),
                            align: 'center',
                            table: table,
                            formatter: Table.api.formatter.toggle,
                            searchList: {'1': '正常', '0        ': '隐藏'}
                        },
                        {field: 'type_name', title: __('模版名称'), operate: false},
                        {field: 'browse_num', title: __('浏览数量'), operate: false},
                        {field: 'like_num', title: __('点赞数量'), operate: false},
                        {field: 'type_status', title: __('模版类型'),formatter: Table.api.formatter.status, searchList: {'1': '个人模板', '2': '企业公司', '3': '实体店铺', '4': '电商行业'}, operate: false},
                        {field: 'preview_img', title: __('名片图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'add_time', title: __('创建时间'), formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            width: 200,
                            buttons: [
                                {
                                    name: '新闻管理',
                                    text: __('新闻管理'),
                                    extend:'data-area=["70%","90%"]',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: function (row) {
                                        return 'mp_mp/news?ids='+row.id;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.news==1){
                                            return true;
                                        }else {
                                            return false;
                                        }
                                    },
                                }, {
                                    name: '产品管理',
                                    text: __('产品管理'),
                                    extend:'data-area=["70%","90%"]',
                                    classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                    url: function (row) {
                                        return 'mp_mp/product?ids='+row.id;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.product==1){
                                            return true;
                                        }else {
                                            return false;
                                        }
                                    },
                                }
                            ],
                        }
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);

            // 为表格绑定事件
            Table.api.bindevent(table);

            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });

            $('.commonsearch-table').removeClass('hidden');

        },
        news: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mp_mp/news?ids='+ Config.ids,
                    edit_url: 'mp_mp/news_edit?id='+ Config.ids,
                    del_url: 'mp_mp/news_del?id='+ Config.ids,
                    multi_url: 'mp_mp/multi',
                    dragsort_url: 'ajax/weigh',
                    table: 'mp_mp',
                }
            });
            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'add_time',
                search: false,
                pageSize: 10,
                columns: [
                    [
                        {field: 'img', title: __('封面图'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'title', title: __('标题')},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            width: 200,
                            buttons: [
                               
                            ],
                        }
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);
            // 为表格绑定事件
            Table.api.bindevent(table);
            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });
        },
        product: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'mp_mp/product?ids='+ Config.ids,
                    edit_url: 'mp_mp/product_edit?id='+ Config.ids,
                    del_url: 'mp_mp/product_del?id='+ Config.ids,
                    multi_url: 'mp_mp/multi',
                    dragsort_url: 'ajax/weigh',
                    table: 'mp_mp',
                }
            });
            var table = $("#table");
            var tableOptions = {
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'add_time',
                search: false,
                pageSize: 10,
                columns: [
                    [
                        {field: 'img', title: __('封面图'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'title', title: __('标题')},
                        {field: 'price', title: __('价格')},
                        {field: 'content', title: __('内容')},
                        {field: 'category', title: __('分类')},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            width: 200,
                            buttons: [
                               
                            ],
                        }
                    ]
                ]
            };
            // 初始化表格
            table.bootstrapTable(tableOptions);
            // 为表格绑定事件
            Table.api.bindevent(table);
            //绑定TAB事件
            $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                // var options = table.bootstrapTable(tableOptions);
                var typeStr = $(this).attr("href").replace('#', '');
                var options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    // params.filter = JSON.stringify({type: typeStr});
                    params.type = typeStr;

                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;

            });
        },
        
        product_edit: function () {
            Controller.api.bindevent();
        },
        news_edit: function () {
            Controller.api.bindevent();
        },
        add: function () {
            Controller.api.bindevent();
            setTimeout(function () {
                $("#c-type").trigger("change");
            }, 100);
        },
        
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                $(document).on("change", "#c-type", function () {
                    $("#c-pid option[data-type='all']").prop("selected", true);
                    $("#c-pid option").removeClass("hide");
                    $("#c-pid option[data-type!='" + $(this).val() + "'][data-type!='all']").addClass("hide");
                    $("#c-pid").data("selectpicker") && $("#c-pid").selectpicker("refresh");
                });
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
